import {mapActions} from 'vuex';
import Vue from 'vue'
import  GlobalFuncDasConstants from './GlobalFuncDasConstants'
import HomeApi from '@/api/HomeApi';  // 引入定义的方法
function GlobalFunc(){}

GlobalFunc.text1=function(param) {
  console.log("嘻嘻");
}
/**
 * 获取当前时间
 * @param date
 * @returns {string}
 * @constructor
 */
GlobalFunc.getTime=function(param) {
  let myDate = new Date();
  let hours = String(myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours());
  let minutes = String(myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes());
  let seconds = String(myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds());
  return hours + ':' + minutes + ':' + seconds;
}
GlobalFunc.CurentTimeT=function(date){
  var now;
  if (date == undefined) {
    now = new Date();
  } else {
    now = new Date(date);
  }
  var year = now.getFullYear();    //年
  var month = now.getMonth() + 1;    //月
  var day = now.getDate();    //日

  var hh = now.getHours();        //时
  var mm = now.getMinutes();         //分
  var ss = now.getSeconds();         //秒

  var clock = year + '-';

  if (month < 10) {
    clock += '0';
  }

  clock += month + '-';

  if (day < 10) {
    clock += '0';
  }

  clock += day + ' ';

  if (hh < 10) {
    clock += '0';
  }

  clock += hh + ':';
  if (mm < 10) clock += '0';
  clock += mm;

  /*if (ss < 10) clock += '0';
  clock += ss;*/
  return (clock);
}
//时分秒时间
GlobalFunc.CurentTimeSFM=function(date){
  var now;
  if (date == undefined) {
    now = new Date();
  } else {
    now = new Date(date);
  }
  var year = now.getFullYear();    //年
  var month = now.getMonth() + 1;    //月
  var day = now.getDate();    //日

  var hh = now.getHours();        //时
  var mm = now.getMinutes();         //分
  var ss = now.getSeconds();         //秒

  var clock = year + '-';

  if (month < 10) {
    clock += '0';
  }

  clock += month + '-';

  if (day < 10) {
    clock += '0';
  }

  clock += day + ' ';

  if (hh < 10) {
    clock += '0';
  }

  clock += hh + ':';
  if (mm < 10) clock += '0';
  clock += mm + ':';

  if (ss < 10) clock += '0';
  clock += ss;
  return (clock);
}
/**
 * 获取英文格式当前时间
 * @param date
 * @returns {string}
 * @constructor
 */
GlobalFunc.CurentTime=function(date) {
  var now;
  if (date == undefined) {
    now = new Date();
  } else {
    now = new Date(date);
  }
  var year = now.getFullYear();    //年
  var month = now.getMonth() + 1;    //月
  var day = now.getDate();    //日

  var hh = now.getHours();        //时
  var mm = now.getMinutes();         //分
  var ss = now.getSeconds();         //秒

  var clock = year + '/';

  if (month < 10) {
    clock += '0';
  }

  clock += month + '/';

  if (day < 10) {
    clock += '0';
  }

  clock += day + ' ';

  if (hh < 10) {
    clock += '0';
  }

  clock += hh + ':';
  if (mm < 10) clock += '0';
  clock += mm;

  /*if (ss < 10) clock += '0';
  clock += ss;*/
  return (clock);
}
/**
 * 获取中文格式当前时间
 * @param date
 * @returns {string}
 * @constructor
 */
GlobalFunc.timeFormatZn=function(time) {
  var date, Y, M, D, h, m, s;
  date = new Date(time);
  Y = date.getFullYear() + '年';
  M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
  D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + '日';
  h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + '时';
  m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + '分';
  s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) + '秒';
  return Y + M + D + h + m + s;
}/**
 * 获取中文格式当前时间
 * @param date
 * @returns {string}
 * @constructor
 */
GlobalFunc.timeFormatRow=function(time) {
  var date, Y, M, D, h, m, s;
  date = new Date(time);
  Y = date.getFullYear() + '-';
  M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + ' ';
  h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return Y + M + D + h + m + s;
}
GlobalFunc.timeFormatRowEn=function(time) {
  var date, Y, M, D, h, m, s;
  date = new Date(time);
  Y = date.getFullYear() + '-';
  M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  D = (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()) + ' ';
  h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  return Y + M + D + h + m + s;
}
/**
 * 获得字符长度
 * @param val
 * @returns {number}
 */
GlobalFunc.getStringLength=function(val) {
  var str = new String(val);
  var bytesCount = 0;
  for (var i = 0, n = str.length; i < n; i++) {
    var c = str.charCodeAt(i);
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
      bytesCount += 1;
    } else {
      bytesCount += 2;
    }
  }
  return bytesCount;
}
/**
 * 鼠标悬停显示TITLE
 * @params     obj        当前悬停的标签
 *
 */
GlobalFunc.titleMouseOver=function titleMouseOver(event,words_per_line) {
  console.log(event);
  //无TITLE悬停，直接返回
  if(typeof event.target.title == 'undefined' || event.target.title == '') return false;
  //不存在title_show标签则自动新建
  var title_show = document.getElementById("title_show");
  if(title_show == null){
    title_show = document.createElement("div");                            //新建Element
    document.getElementsByTagName('body')[0].appendChild(title_show);    //加入body中
    var attr_id = document.createAttribute('id');                        //新建Element的id属性
    attr_id.nodeValue = 'title_show';                                    //为id属性赋值
    title_show.setAttributeNode(attr_id);                                //为Element设置id属性
    document.getElementById("title_show").classList.add("title_focus_toolTip");//为为Element添加类属性
    var attr_style = document.createAttribute('style');                    //新建Element的style属性
    attr_style.nodeValue = 'position:absolute;'                            //绝对定位
      /*  +'border:solid 1px #f3f3f3; background:rgba(50, 50, 50, 0.701961)'                //边框、背景颜色
        +'top:-50%'                //边框、背景颜色
        +'border-radius:2px;box-shadow:0px 0px 2px #ccc;'                //圆角、阴影
        +'line-height:30px!important;'                                            //行间距
        */
        +'font-size:14px; padding: 2px 5px;z-index:9999';                            //字体大小、内间距
    try{
      title_show.setAttributeNode(attr_style);                        //为Element设置style属性
    }catch(e){
      //IE6
      // title_show.style.position = 'absolute';
      // title_show.style.border = 'solid 1px #f3f3f3';
      // title_show.style.background = 'rgba(50, 50, 50, 0.701961)';
      // title_show.style.lineHeight = '20px';
      // title_show.style.fontSize = '30px';
      // title_show.style.padding = '2px 5px';
      document.getElementById("title_show").classList.add("title_focus_toolTip");//为为Element添加类属性
    }
  }
  //存储并删除原TITLE
  document.title_value = event.target.title;
  event.target.title = '';
  //单行字数未设定，非数值，则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 50;
  }
  //格式化成整形值
  words_per_line = parseInt(words_per_line);
  //在title_show中按每行限定字数显示标题内容，模拟TITLE悬停效果
  title_show.innerHTML = GlobalFunc.split_str(document.title_value,words_per_line);
  //显示悬停效果DIV
  title_show.style.display = 'block';

  //根据鼠标位置设定悬停效果DIV位置
  event = event || window.event;                            //鼠标、键盘事件
  var top_down = 50;                                        //下移15px避免遮盖当前标签
  //最左值为当前鼠标位置 与 body宽度减去悬停效果DIV宽度的最小值，否则将右端导致遮盖
  var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth);
  title_show.style.left = left+"px";            //设置title_show在页面中的X轴位置。
  title_show.style.top = (event.clientY - top_down)+"px";    //设置title_show在页面中的Y轴位置。
  // title_show.style.top = top_down+"%";    //设置title_show在页面中的Y轴位置。
}
/**
 * 鼠标离开隐藏TITLE
 * @params    obj        当前悬停的标签
 *
 */
GlobalFunc.titleMouseOut= function titleMouseOut(event) {
  var title_show = document.getElementById("title_show");
  //不存在悬停效果，直接返回
  if(title_show == null) return false;
  //存在悬停效果，恢复原TITLE
  event.target.title = document.title_value;
  //隐藏悬停效果DIV
  title_show.style.display = "none";
}
/**
 * className 类名
 * tagname HTML标签名，如div,td,ul等
 * @return Array 所有class对应标签对象组成的数组
 * @example
 <div class="abc">abc</div>
 var abc = getClass('abc');
 for(i=0;i<abc.length;i++){
     abc[i].style.backgroundColor='red';
 }
 */
GlobalFunc.getClass= function getClass(className,tagname) {
  //tagname默认值为'*'，不能直接写成默认参数方式getClass(className,tagname='*')，否则IE下报错
  if(typeof tagname == 'undefined') tagname = '*';
  if(typeof(getElementsByClassName) == 'function') {
    return getElementsByClassName(className);
  }else {
    var tagname = document.getElementsByTagName(tagname);
    var tagnameAll = [];
    for(var i = 0; i < tagname.length; i++) {
      if(tagname[i].className == className) {
        tagnameAll[tagnameAll.length] = tagname[i];
      }
    }
    return tagnameAll;
  }
}
/**
 * JS字符切割函数
 * @params     string                原字符串
 * @params    words_per_line        每行显示的字符数
 */
GlobalFunc.split_str= function split_str(string,words_per_line) {
  //空串，直接返回
  if(typeof string == 'undefined' || string.length == 0) return '';
  //单行字数未设定，非数值，则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 50;
  }
  //格式化成整形值
  words_per_line = parseInt(words_per_line);
  //取出i=0时的字，避免for循环里换行时多次判断i是否为0
  var output_string = string.substring(0,1);
  //循环分隔字符串
  for(var i=1;i<string.length;i++) {
    //如果当前字符是每行显示的字符数的倍数，输出换行
    // if(i%words_per_line == 0) {
    //     output_string += "<br/>";
    // }
    //每次拼入一个字符
    output_string += string.substring(i,i+1);
  }
  return output_string;
}

/**
 * 悬停事件绑定
 * @params    objs        所有需要绑定事件的Element
 *
 */
GlobalFunc.attachEvent= function attachEvent(objs,words_per_line){
  if(typeof objs != 'object') return false;
  //单行字数未设定，非数值，则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 10;
  }
  for(let i=0;i<objs.length;i++){
    objs[i].onmouseover = function(event){
      GlobalFunc.titleMouseOver(this,event,words_per_line);
    }
    objs[i].onmouseout = function(event){
      GlobalFunc.titleMouseOut(this);
    }
  }
}


GlobalFunc.ttoolTopbody = 'dfdd'
GlobalFunc.getGlobalFunctoolTopbody= function getGlobalFunctoolTopbody(){
  console.log(GlobalFunc.ttoolTopbody);
  return GlobalFunc.ttoolTopbody;
}
/**
 * 自定义鼠标悬浮弹框
 */
GlobalFunc.GLOBALItemMouseover=(e,toolTipId,name)=> {
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("top", e.clientY -140+ "px");
  focusTooltip.css("left", e.clientX + "px");
  var headerHtml =
      "<div style='font-size:14px;color: #ffffff;font-weight: bold;font-family: SourceHanSansCN;pointer-events: none'>" +
      name +
      "</div>";
  var effectHtml =
      "<div style='font-size:12px;margin-top:5px;pointer-events: none'>" + "</div>";
  let params = headerHtml + effectHtml;
  GlobalFuncDasConstants.setToolTopbody(name);
  console.log(GlobalFuncDasConstants.aaoolTopbody);
  GlobalFunc.ttoolTopbody = params;
  focusTooltip.css("display", "block");
  return params;
}
/**
 * 自定义鼠标悬浮弹框关闭
 */
GlobalFunc.GLOBALIitemMouseout=function GLOBALIitemMouseout(toolTipId) {
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("display", "none");
}
/**
 * 自定义鼠标移动
 */
GlobalFunc.GLOBALItemMousemove=function  GLOBALItemMousemove(e,toolTipId,name) {
  var self = this;
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("top", e.clientY - 80 + "px");
  focusTooltip.css("left", e.clientX + 100 + "px");
  var headerHtml =
      "<div style='font-size:12px;color: #fec443;font-weight: bold;font-family: SourceHanSansCN;'>" +
      name +
      "</div>";
  var effectHtml =
      "<div style='font-size:12px;margin-top:5px;'>" + "</div>";
  let params = headerHtml + effectHtml;
  return params;
}
/**
 * 全局写日志函数
 */
GlobalFunc.GlaAddAudit=function GlaAddAudit(getData) {
  console.log("日志参数："+ getData.dcId);
  console.log("日志参数："+ getData.describe);
  /**
   * dcId:调度员的ID
   *type:操作类型 1-调度 2-对讲 3-地图 4-会议 5-业务管理 6-系统管理 7-会话
   * status:操作状态字符串
   * describe:操作描述
   * time:操作时间
   * fleetId:集团ID
   * detaiType:小类 int
   * remark；备注（选填）
   */
  let data ={
    dcId:getData.dcId || null,
    type:getData.type|| null,
    status:getData.status|| null,
    describe:getData.describe|| null,
    time:GlobalFunc.CurentTimeSFM()|| null,
    fleetId:getData.fleetId|| null,
    detailType:getData.detailType|| null,
    remark:getData.remark|| null,
  }
  HomeApi.addAudit(data)
  .then(res => {
    // 返回数据
    var a = 1;

  })
  .catch(err => {
    // 异常情况
    var b = 1;
  })
}
/**
 * 时间字符串转时间格式
 * @param oldDate
 * @returns {string}
 * @constructor
 */
GlobalFunc.GlaChangeDate=function GlaChangeDate(oldDate){
  var dateee = new Date(oldDate).toJSON();//这个ie不兼容，会返回1970年的时间
  var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().
      replace(/T/g, ' ').
      replace(/\.[\d]{3}Z/, '');
  return date;
}
export default {
  // Vue.js的插件应当有一个公开方法 install。这个方法的第一个参数是 Vue 构造器，第二个参数是一个可选的选项对象。
  install(Vue,options) {
    Vue.prototype.GlobalFunc = GlobalFunc;
  }
}
